package com.bawan.vims.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.bawan.vims.model.EmployeeInfo;
import com.bawan.vims.service.IEmployeeService;

/**
 * 
 * <p> Description	: EmployeeController.java </p>
 * <p> Copyright	: Copyright (c) 2015 by FITT</p>
 * <p> Author 		: JOCHEN ZHENG </p>
 * <p> Create		: 2015年3月25日 上午10:42:06 </p> 
 * <p> version 		: 1.0 </p>
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController extends BaseController<EmployeeInfo> {

	@Resource(name = "employeeService")
	private IEmployeeService employeeService;
	
	public EmployeeController() {
		super();		
		setClazz(EmployeeInfo.class);
	}

	@RequestMapping("/count")
	public ModelAndView count(HttpServletRequest request) {
		int count = employeeService.findAll().size();

		ModelAndView mva = new ModelAndView();
		mva.addObject("employeeCount", count);
		mva.setViewName("employee/employeeCount");

		return mva;
	}

	@RequestMapping("/getList")
	public ModelAndView getList(Model model) {
		ModelAndView mv = new ModelAndView();
		List<EmployeeInfo> list = employeeService.findAll();

		logger.info("log======table 'employeeInfo' all records:" + list.size());

		mv.addObject("employeeList", list);
		mv.setViewName("employee/employeeList");
		return mv;
	}

	@RequestMapping("/findAll")
	public String findAll(HttpServletRequest request) {
		request.setAttribute("employeeList", employeeService.findAll());

		return "/employee/employeeList";
	}
	
	@RequestMapping("/input")
	public String input(HttpServletRequest request) {
		EmployeeInfo entity = getEntity(request);
		if (entity != null) {
			entity = employeeService.findById(entity.getEmployeeId());
			request.setAttribute("employee", entity);
		}
		return "/employee/employeeAdd";
	}
	
	@RequestMapping("/add")
	public String save(HttpServletRequest request) {
		logger.info(this.getClassNameAndMethod() + " info:" + System.currentTimeMillis());
		if (employeeService.save(getEntity(request)))
			return findAll(request);
		else {
			request.setAttribute("errorMsg", "新增雇员操作失败!");
			return "/employee/employeeAdd";
		}
	}
	
	@RequestMapping("/delete")
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info(this.getClassNameAndMethod() + " info:" + System.currentTimeMillis());
		PrintWriter out = response.getWriter();  
		Map<String, String> reply = new HashMap<String, String>();
		String employeIds = request.getParameter("employeeIds");
		if (employeIds == null || "".equals(employeIds)) {
			reply.put("replyCode", "-1");
			reply.put("replyMsg", "删除失败：参数错误!");
		} else {
			String[] ids = employeIds.split(",");
			int success = 0;
			for (String id : ids) {
				employeeService.deleteById(id);
				success ++;
			}
			reply.put("replyCode", "0");
			reply.put("replyMsg", "成功删除 " + success + "条数据!");
		}
		
		out.print(JSONObject.toJSON(reply).toString());
	}
	
	@RequestMapping("/login")
	public String login(HttpServletRequest request) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		if ("admin".equals(username) && "admin".equals(password)) {
			return findAll(request);
		} else {
			return "redirect:/index.jsp";
		}
	}
}
